const app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    modal: {
      toastHidden: true,
      msg: "当前网格异常，请稍后再试"
    },
    currentTab: 0, //预设当前项的值
    winWidth: 0,// 页面配置 
    winHeight: 0,
    tab: [
      { title: "未使用" },
      { title: "已使用" }
    ],
    discontent: [
      // { money: "10", date:"8.17~8.30"},
    ],
    content: [
      // { money: "10", date: "8.17~8.30" },
    ],
    pageNum: 1,
    pageSize: 10

  },
  onLoad: function (options) {
    var that = this;
    /* 获取系统信息 */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

  },
  /*生命周期函数--监听页面显示*/
  onShow: function () {
    var that = this;
    var data = {
      crcpnUser: app.globalData.userUuid,
      crcpnUseStatus: 2,
      pageNum: that.data.pageNum,
      pageSize: that.data.pageSize
    };
    util.requestGet("coreCoupon/find/my/page", data, function (res) {
      if (res.success) {
        var new_content = [];
        var obj = {};
        for (var i = 0; i < res.data.result.length; i++) {
          var obj = {};
          obj.money = res.data.result[i].crcpnReduce;
          obj.date = new Date(res.data.result[i].crcpnStart).toLocaleDateString().substr(5) + "~" + new Date(res.data.result[i].crcpnEnd).toLocaleDateString().substr(5);
          new_content.push(obj);
        }
        that.setData({ "discontent": new_content });
      } 
    }, function (errData) {
      that.setData({ "modal.toastHidden": false, "modal.msg": '当前网格异常，请稍后再试' });
    });

    var data1 = {
      crcpnUser: app.globalData.userUuid,
      crcpnUseStatus: 1,
      pageNum: that.data.pageNum,
      pageSize: that.data.pageSize
    };
    util.requestGet("coreCoupon/find/my/page", data1, function (res) {
      console.log(res)
      if (res.success) {
        var new_content = [];
        var obj = {};
        for (var i = 0; i < res.data.result.length; i++) {
          var obj = {};
          obj.money = res.data.result[i].crcpnReduce;
          obj.date = new Date(res.data.result[i].crcpnStart).toLocaleDateString().substr(5) + "~" + new Date(res.data.result[i].crcpnEnd).toLocaleDateString().substr(4);
          new_content.push(obj);
        }
      }
      else {

      }
      that.setData({ "content": new_content });
    }, function (errData) {
      that.setData({ "modal.toastHidden": false, "modal.msg": '当前网格异常，请稍后再试' });
    });
  },
  /*滑动切换tab */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  switchTab(e) {
    // data - current与e.currentTarget.dataset.current是一一对应的值
    var that = this;
    var cur = e.currentTarget.dataset.current;
    that.setData({ currentTab: cur });
  },
  toastChange() {
    this.setData({
      duration: e.detail.value
    })
  }
})